package com.shenmazong.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shenmazong.pojo.TbTaskChapter;
import com.shenmazong.service.TbTaskChapterService;
import com.shenmazong.mapper.TbTaskChapterMapper;
import com.shenmazong.utils.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class TbTaskChapterServiceImpl extends ServiceImpl<TbTaskChapterMapper, TbTaskChapter>
implements TbTaskChapterService{
    @Autowired
    TbTaskChapterMapper tbTaskChapterMapper;

    @Override
    public PageInfo findTbTaskChapterList(Integer pageNum, Integer pageSize, Object taskChapter) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbTaskChapter> tbTaskChapters=tbTaskChapterMapper.findTbTaskChapterList(taskChapter);
        PageInfo<TbTaskChapter> pageInfo = new PageInfo<>(tbTaskChapters);
        return pageInfo;
    }

    @Override
    public ResultResponse saveTaskChapter(TbTaskChapter tbTaskChapter) {
        tbTaskChapter.setCreateTime(new Date());
        tbTaskChapter.setUpdateTime(new Date());
        int i = tbTaskChapterMapper.insert(tbTaskChapter);
        if(i<0){
            return ResultResponse.FAILED(-1,"添加失败");
        }
        return ResultResponse.SUCCESS("添加成功");
    }

    @Override
    public ResultResponse echoTaskChapter(TbTaskChapter tbTaskChapter) {
        TbTaskChapter taskChapter = tbTaskChapterMapper.selectById(tbTaskChapter.getChapterId());
        if(taskChapter==null){
            return ResultResponse.FAILED(-1,"回显失败");
        }
        return ResultResponse.SUCCESS(taskChapter);
    }

    @Override
    public ResultResponse updateTaskChapter(TbTaskChapter tbTaskChapter) {
        tbTaskChapter.setUpdateTime(new Date());
        int i = tbTaskChapterMapper.updateById(tbTaskChapter);
        if(i<0){
            return ResultResponse.FAILED(-1,"修改失败");
        }
        return ResultResponse.SUCCESS("修改成功");
    }

    @Override
    public ResultResponse delTaskChapter(TbTaskChapter tbTaskChapter) {
        try {
            tbTaskChapterMapper.deleteById(tbTaskChapter.getChapterId());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED(-1,"删除失败");
        }
        return ResultResponse.SUCCESS("删除成功");
    }

    @Override
    public ResultResponse findTaskChapter() {
        List<TbTaskChapter> tbTaskChapters = tbTaskChapterMapper.selectList(null);
        return ResultResponse.SUCCESS(tbTaskChapters);
    }
}




